import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;

public final class Doer extends JFrame{

  private final NewItem newItem=new NewItem("New",this);
  private final OpenItem openItem=new OpenItem("Open",this);
  private final SaveItem saveItem=new SaveItem("Save",this);
  private final SaveAsItem saveAsItem=new SaveAsItem("Save As...",this);
  private final SaveAllItem saveAllItem=new SaveAllItem("Save All",this);
  private final CloseItem closeItem=new CloseItem("Close",this);
  private final CloseAllItem closeAllItem=new CloseAllItem("Close All",this);
  //set
  private final FontItem fontItem=new FontItem("Font",this);
  //build
  private final CompileItem compileItem=new CompileItem("Compile",this);
  private final RunItem runItem=new RunItem("Run",this);
		     //edit
  private final FindItem findItem=new FindItem("Find",this);
  private final FindNextItem findNextItem=new FindNextItem("Find Next",this);
  private final CopyItem copyItem=new CopyItem("Copy",this);
  private final CutItem cutItem=new CutItem("Cut",this);
  private final PasteItem pasteItem=new PasteItem("Paste",this);
  private final UndoItem undoItem=new UndoItem("Undo",this);
  private final RedoItem redoItem=new RedoItem("Redo",this);


  private Font font=new Font("DialogInput",Font.PLAIN,12);

  private int select=0;
  private String findWord=new String(),
                 direction=new String();

  private final JMenu fileMenu=new JMenu("File"),
                      setMenu=new JMenu("Set"),
		      editMenu=new JMenu("Edit"),
		      buildMenu=new JMenu("Build");

  private final JToolBar toolBar=new JToolBar();
  private final CMDInfo CMDInfoPane=new CMDInfo(this);
  private final FileTree fileTree=new FileTree(this);
  private final JTabbedPane editPane=new JTabbedPane(JTabbedPane.BOTTOM,JTabbedPane.SCROLL_TAB_LAYOUT),
                            leftPane=new JTabbedPane(JTabbedPane.TOP,JTabbedPane.SCROLL_TAB_LAYOUT);
  private final StatusBar statusBar=new StatusBar(this);

  private final Obs obs=new Obs();

  //private Window windowSplash;

  public Doer() {
    super("Doer 1.0");
    //prepareSplash();
    //startSplash();
    getDefault();

    addItem(fileMenu,newItem,'N');
    addItem(fileMenu,openItem,'O');
    addItem(fileMenu,saveItem,'S');
    addItem(fileMenu,saveAsItem);
    addItem(fileMenu,saveAllItem);
    addItem(fileMenu,closeItem,'W');
    addItem(fileMenu,closeAllItem,'C');

    saveItem.setEnabled(false);
    saveAsItem.setEnabled(false);
    saveAllItem.setEnabled(false);
    closeItem.setEnabled(false);
    closeAllItem.setEnabled(false);
    //setMenu
    addItem(setMenu,fontItem,'T');
    //buildMenu
    addItem(buildMenu,compileItem,'P');
    addItem(buildMenu,runItem,'U');

    compileItem.setEnabled(false);
    runItem.setEnabled(false);
    //editMenu
    addItem(editMenu,findItem,'F');
    addItem(editMenu,findNextItem,'R');

    editMenu.addSeparator();
    
    addItem(editMenu,copyItem);
    addItem(editMenu,cutItem);
    addItem(editMenu,pasteItem);
    
    editMenu.addSeparator();
    
    addItem(editMenu,undoItem,'Z');
    addItem(editMenu,redoItem,'Y');
    
    findItem.setEnabled(false);
    findNextItem.setEnabled(false);
    copyItem.setEnabled(false);
    cutItem.setEnabled(false);
    pasteItem.setEnabled(false);
    undoItem.setEnabled(false);
    redoItem.setEnabled(false);
    //toolBar
    addButton(newItem);
    addButton(openItem);
    addButton(saveItem);
    addButton(closeItem);

    toolBar.addSeparator();

    addButton(copyItem);
    addButton(cutItem);
    addButton(pasteItem);
    
    toolBar.addSeparator();

    addButton(undoItem);
    addButton(redoItem);
    
    toolBar.addSeparator();

    addButton(compileItem);
    addButton(runItem);
    //
    JMenuBar menuBar=new JMenuBar();
    setJMenuBar(menuBar);
    fileMenu.setMnemonic('F');
    setMenu.setMnemonic('S');
    buildMenu.setMnemonic('B');
    editMenu.setMnemonic('E');
    //
    menuBar.add(fileMenu);
    menuBar.add(editMenu);
    menuBar.add(buildMenu);
    menuBar.add(setMenu);

    editPane.addChangeListener(new ChangeListener(){
                                 public void stateChanged(ChangeEvent e){
				   select=editPane.getSelectedIndex();
				   if(select>-1){
                                       InterWindow interWindow=(InterWindow)(editPane.getComponentAt(select));
                                       saveItem.setEnabled(!interWindow.isSaved());
                                       setTitle(editPane.getTitleAt(select)+(interWindow.isSaved()==true?' ':'*'));
                                       statusBar.getCD().setText("cd: "+interWindow.getDirection());
                                   }
				 }
                               });

    leftPane.addTab("File",fileTree);

    JSplitPane vSplitPane=new JSplitPane(JSplitPane.VERTICAL_SPLIT,true,editPane,CMDInfoPane);
    vSplitPane.setOneTouchExpandable(true);
    vSplitPane.setDividerLocation(400);

    JSplitPane hSplitPane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,leftPane,vSplitPane);
    hSplitPane.setOneTouchExpandable(true);
    hSplitPane.setDividerLocation(100);

    Container content = getContentPane();
    content.add(hSplitPane);
    content.add(toolBar,BorderLayout.NORTH);
    content.add(statusBar,BorderLayout.SOUTH);

    //property
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        closeAllItem.closeAllFile();
		setDefault();
        System.exit(0);
      }
    });
    Dimension size=getToolkit().getScreenSize();
    setBounds(10,10,2*size.width/3,2*size.height/3);
    setFont(font);
    show();
    //stopSplash();
  }

  //class Observable
  class Obs extends Observable{
    public void setFont(Font font){
      setChanged();
      notifyObservers(font);
    }
  }

  //methord
  public JMenuItem addItem(JMenu menu,Action item){
     return menu.add(item);
  }
  public JMenuItem addItem(JMenu menu,Action item,char key){
    JMenuItem menuItem=addItem(menu,item);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(key,Event.CTRL_MASK));
    return menuItem;
  }
  public JButton addButton(Action action){
    JButton button=toolBar.add(action);
    button.setToolTipText((String)action.getValue(action.SHORT_DESCRIPTION));
    return button;
  }
  public JTabbedPane getTab(){
    return editPane;
  }
  public String getWord(){
    return findWord;
  }
  public void setWord(String s){
    findWord=s;
  }
  public int getSelect(){
    return select;
  }
  public Obs getObs(){
    return obs;
  }
  public SaveItem getSaveItem(){
    return saveItem;
  }
  public SaveAsItem getSaveAsItem(){
    return saveAsItem;
  }
  public SaveAllItem getSaveAllItem(){
    return saveAllItem;
  }
  public CloseItem getCloseItem(){
    return closeItem;
  }
  public CloseAllItem getCloseAllItem(){
    return closeAllItem;
  }
  public NewItem getNewItem(){
    return newItem;
  }
  public FindItem getFindItem(){
    return findItem;
  }
  public FindNextItem getFindNextItem(){
    return findNextItem;
  }
  public CompileItem getCompileItem(){
    return compileItem;
  }
  public RunItem getRunItem(){
    return runItem;
  }
  public CopyItem getCopyItem(){
    return copyItem;
  }
  public CutItem getCutItem(){
    return cutItem;
  }
  public PasteItem getPasteItem(){
    return pasteItem;
  }
  public UndoItem getUndoItem(){
    return undoItem;
  }
  public RedoItem getRedoItem(){
    return redoItem;
  }
  public StatusBar getStatusBar(){
    return statusBar;
  }
  public CMDInfo getCMDInfo(){
    return CMDInfoPane;
  }
  public FileTree getFileTree(){
    return fileTree;
  }

  public String getDirection(){
    return direction;
  }
  public void setDirection(String dir){
    direction=dir;
  }
  public void getDefault(){
    try{
      File file=new File("Doer.ini");
      if(file.exists()){
        BufferedReader br=new BufferedReader(new FileReader(file));
	direction=br.readLine();
	br.close();
      }
    }
    catch(Exception e){System.err.println(e);}
  }
  public void setDefault(){
    try{
      File file=new File("Doer.ini");
      FileWriter fw=new FileWriter(file);
      fw.write(direction);
      fw.flush();
      fw.close();
    }
    catch(Exception e){System.err.println(e);}
  }
  /*private void prepareSplash(){
    Toolkit toolkit=Toolkit.getDefaultToolkit();
    windowSplash=new Window(this);
    Image image=toolkit.getImage("images"+File.separator+"splash.jpg");
    ImageCanvas canvas=new ImageCanvas(image);
    windowSplash.add(canvas,"Center");
    Dimension scmSize=toolkit.getScreenSize();
    int imgWidth=image.getWidth(this);
    int imgHeight=image.getHeight(this);
    windowSplash.setLocation(scmSize.width/2-(imgWidth/2),scmSize.height/2-(imgHeight/2));
    windowSplash.setSize(imgWidth,imgHeight);
  }
  private void startSplash(){
    windowSplash.setVisible(true);
    windowSplash.toFront();
  }
  private void stopSplash(){
    windowSplash.dispose();
  }
  //
   class ImageCanvas extends Canvas {
     Image image;

     public ImageCanvas(Image image) {
       this.image = image;

       MediaTracker mt = new MediaTracker(this);
       try {
         mt.addImage(image, 0);
         mt.waitForID(0);
       }
      catch(InterruptedException ex) {
        ex.printStackTrace();
      }
    }
    }*/
  public static void main(String[] args) {
    new Doer();
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {}
  }
}
